<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css" media="screen">
		* {
            margin: 0;
            padding: 0;
            text-decoration: none;
        }
        
        body {
            padding: 20px;
        }
        
        #container {
            position: relative;
            width: 600px;
            height: 400px;
            border: 3px solid #333;
            overflow: hidden;
        }
        
        #list {
            position: absolute;
            z-index: 1;
            width: 4200px;
            height: 400px;
        }
        
        #list img {
            float: left;
            width: 600px;
            height: 400px;
        }
        
        #buttons {
            position: absolute;
            left: 250px;
            bottom: 20px;
            z-index: 2;
            height: 10px;
            width: 100px;
            list-style: none;
            float: left;
        }
        
        #buttons li {
            float: left;
            margin-right: 5px;
            width: 10px;
            height: 10px;
            border: 1px solid #fff;
            border-radius: 50%;
            background: #333;
            cursor: pointer;
        }
        
        #buttons .on {
            background: orangered;
        }
        
        .arrow {
            position: absolute;
            top: 180px;
            z-index: 2;
            display: none;
            width: 40px;
            height: 40px;
            font-size: 36px;
            font-weight: bold;
            line-height: 39px;
            text-align: center;
            color: #fff;
            background-color: rgba(0, 0, 0, .3);
            cursor: pointer;
        }
        
        .arrow:hover {
            background-color: rgba(0, 0, 0, .7);
        }
        
        #container:hover .arrow {
            display: block;
        }
        
        #prev {
            left: 20px;
        }
        
        #next {
            right: 20px;
        }
	</style>
	<script type="text/javascript">
		window.onload = function() {
            var list = document.getElementById('list');
            var prev = document.getElementById('prev');
            var next = document.getElementById('next');

            function animate(offset) {
                //获取的是style.left，是相对左边获取距离，所以第一张图后style.left都为负值，
                //且style.left获取的是字符串，需要用parseInt()取整转化为数字。
                var newLeft = parseInt(list.style.left) + offset;
                list.style.left = newLeft + 'px';
				 if(newLeft<-3000){
				      list.style.left = -600 + 'px';
				 }
				 if(newLeft>-600){
				      list.style.left = -3000 + 'px';
				 }
            }

            prev.onclick = function() {             
                animate(600);
            }
            next.onclick = function() {  
                animate(-600);
            }
            var timer;
   			 function play() {
                //将轮播图换成向右切换图片
                timer = setInterval(function () {
                    next.onclick();
                }, 2000)
            }

   			play();

   			var container = document.getElementById('container');

            function stop() {
                clearInterval(timer);
            }

            container.onmouseover = stop;
            container.onmouseout = play;

            var buttons = document.getElementById('buttons').getElementsByTagName('li');
            var index = 1;

            function buttonsShow() {
                //这里需要清除之前的样式
                for (var i = 0; i < buttons.length; i++) {
                    if (buttons[i].className == 'on') {
                        buttons[i].className = '';
                    }
                }
                //数组从0开始，故index需要-1
                buttons[index - 1].className = 'on';
            }

            prev.onclick = function() {
                index -= 1;
                if (index < 1) {
                    index = 5;
                }
                buttonsShow();
                animate(600);
            }

            next.onclick = function() {
                //由于上边定时器的作用，index会一直递增下去，我们只有5个小圆点，所以需要做出判断
                index += 1;
                if (index > 5) {
                    index = 1;
                }
                buttonsShow();
                animate(-600);
            }

            for (var i = 0; i < buttons.length; i++) {
                // 这里使用的是立即执行函数，
                (function(i) {
                    buttons[i].onclick = function() {
                        var clickIndex = parseInt(this.getAttribute('index'));
                        var offset = 600 * (index - clickIndex); 
                        animate(offset);
                        index = clickIndex;
                        buttonsShow();
                    }
                })(i)
            }
        }
	</script>
</head>
<body>
	<div id="container">
        <div id="list" style="left: -600px;">
            <img src="images/wf5.jpg" />
            <img src="images/wf1.jpg" />
            <img src="images/wf2.jpg" />
            <img src="images/wf3.jpg" />
            <img src="images/wf4.jpg" />
            <img src="images/wf5.jpg" />
            <img src="images/wf1.jpg" /> 
        </div>
        <ul id="buttons">
        	<li class="on"></li>
        	<li></li>
        	<li></li>
        	<li></li>
        	<li></li>
        </ul>
        <a  id="prev" class="arrow"><</a>
        <a  id="next" class="arrow">></a>
    </div>
</body>
</html>